<template>
	<view>
		<scroll-view scroll-y scroll-with-animation style="height: calc(100vh - 50px)" :show-scrollbar="false"
			@scrolltolower="loadMore" @scrolltoupper="refresh">
			<view class="mt-1 pl-3 pr-3 dfex lineHight-3 border-f5f5f5" v-for="(notice, index) in notices" :key="index"
				@click="getDiClick(notice.id)">
				<view class="dfes">
					<view v-if="!notice.isRead" style="color: #FF0000; padding-right: 5px;">●</view>
					<view class="wrap" @click="readNotice(notice.id, index)"
						:style="{ color: notice.isRead ? '#999999' : '#333' }">{{ notice.title }}</view>
				</view>
				<view class="text-999999 font-14" style="width: 100px; text-align: right;">
					{{notice.updateTime.slice(0, 10)}}
				</view>
			</view>
			<view v-if="!hasMore && notices.length > 0" class="text-center mt-3 mb-3">
				<view class="text-muted">没有更多数据了</view>
			</view>
			<view v-else-if="!hasMore && notices.length === 0" class="text-center mt-3 mb-3">
				<view class="text-muted">暂无数据</view>
			</view>
			<view v-else class="text-center mt-3 mb-3">
				<view class="btn btn-primary">加载更多</view>
			</view>
		</scroll-view>
	</view>
</template>


<script>
	import $H from '@/common/free-lib/request.js';
	import freeNavBar from "@/components/free-ui/free-nav-bar.vue"
	export default {
		components: {
			freeNavBar
		},
		data() {
			return {
				notices: [],
				form: {
					pageNo: 1,
					pageSize: 10,
					total: 0 // 总页数
				},
				hasMore: true, // 是否还有更多数据
			}
		},
		onLoad() {
			this.NewList()
		},
		methods: {
			setDefaultPageConfig() {
				this.form.pageNo = 1
				this.form.pageSize = 10
				this.form.total = 0
			},
			// 浏览记录
			readNotice(id, index) {
				$H.get('/basisInform/platformNotice/agriculture/clickRead', {
					id: id
				}, {}).then(res => {
					this.notices[index].isRead = true; // 点击浏览后将未读状态设置为已读
					if (this.notices[index].isRead) {
						uni.hideTabBarRedDot({
							index: 1
						}); // 隐藏红点
					}
				}).catch(error => {
					uni.showToast({
						title: "网络错误，请稍后再试",
						icon: 'none'
					});
				});
				uni.hideTabBarRedDot({
					index: 1
				}); // 隐藏红点
			},
			getDiClick(id) {
				uni.navigateTo({
					url: `/pages/tabbar/index/NewListDi?id=${id}`
				})
			},
			NewList() {
				$H.get('/basisInform/platformNotice/agriculture/list', {
					...this.form
				}, {}).then(res => {
					this.notices = res.result.records
					this.form.total = res.result.total;
					this.hasMore = this.notices.length < this.form.total;
				}).catch(error => {
					uni.showToast({
						title: "网络错误，请稍后再试",
						icon: 'none'
					});
				});
			},
			// 加载更多
			loadMore() {

				if (this.form.pageNo * this.form.pageSize >= this.form.total) {
					this.hasMore = false;
					return;
				}
				this.form.pageNo++;
				// 请求数据
				this.NewList();
			},
			// 下拉刷新
			refresh() {
				// 重置页码为 1
				this.form.pageNo = 1;
				// 清空数据列表
				this.notices = [];
				// 设置标记为有更多数据
				this.hasMore = true;
				// 重新请求数据
				this.NewList();
				// 结束下拉刷新动画
				uni.$emit('stopPullDownRefresh');
			},
		}
	}
</script>
<style>
</style>